package com.ysxsoft.wdg.ui.activity.my.partner;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.my.QuestionAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.response.HomeProductResponse;
import com.ysxsoft.wdg.bean.response.QuestionResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.statusbar.StatusBarUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

public class CommonQuestionActivity extends BaseActivity {

    public static void start(Context context){
        Intent intent = new Intent(context,CommonQuestionActivity.class);
        context.startActivity(intent);
    }


    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;


    private QuestionAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_list2;
    }

    @Override
    protected void initData() {
        StatusBarUtil.setStatusBarTextColor(this,true);
        initStatusBar(topView,true);
        titleContent.setText("常见问题");

        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new QuestionAdapter(R.layout.item_question);
        recyclerview.setAdapter(adapter);
        getList();
    }

    @Override
    protected void setListener() {

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                QuestionResponse.DataBean dataBean= (QuestionResponse.DataBean) adapter.getItem(position);
                CommonQustDetailActivity.start(mContext, dataBean.getQid()+"");
            }
        });

        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                getList();
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                getList();
            }
        });
    }

    @OnClick(R.id.title_finish)
    public void onViewClicked() {
        finish();
    }

    private int page = 1;

    /**
     * 获取问题列表
     */
    private void getList() {
        if(page==1){
            multStatusView.showLoading();
        }
        OkHttpUtils.post()
                .url(UrlHost.QUESTION)
                .addParams("page",page+"")
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                    multStatusView.showError();
                    reset();
                }
            }

            @Override
            public void onResponse(String response, int id) {
                reset();
                if (response != null) {
                    QuestionResponse homeProductResponse = JsonUtils.parseByGson(response, QuestionResponse.class);
                    if (homeProductResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(homeProductResponse.getCode())) {
                        List<QuestionResponse.DataBean> list = homeProductResponse.getData();
                        if (page == 1) {
                            multStatusView.showContent();
                            if (list != null && !list.isEmpty()) {
                            } else {
                                multStatusView.showEmpty();
                            }
                            adapter.setNewData(list);
                        } else {
                            adapter.addData(list);
                        }
                    }else{
                        ToastUtils.showToast(homeProductResponse.getMs());
                    }
                } else {
                    multStatusView.showError();
                }
            }
        });
    }

    private void reset() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh(true);
        }
        refreshLayout.finishLoadmore();
    }

}
